package org.jingle.sketch.component
{
	import org.jingle.sketch.GraphicsUtils;
	
	public class Line extends Composite
	{
		private var _destination:Point;
		private var _dashed:Boolean;
		
		public function Line(parent:Composite, x:int, y:int, x1:int, y1:int) {
			super(parent);
			this.origin = new Point(x, y);
			_destination = new Point(x1, y1); 
		}

		public override function init():void {
			sprite.graphics.clear();
			sprite.graphics.lineStyle(thickness, lineColor, alpha);
			if(dashed) {
				var width:int = _destination.x - origin.x;
				var height:int = _destination.y - origin.y;
				var ruler:int = width;
				if(width < height){
					ruler = height;
				}
				var count:int = ruler / 2;
				var stepx:int = width / count;
				var stepy:int = height / count;
				
				for(var i:int=0; i<count; i+= 2){
					GraphicsUtils.drawLine(sprite.graphics, origin.x + i*stepx, origin.y + i*stepy, origin.x + i*stepx + stepx, origin.y + i*stepy +  stepy);
				}
				
			} else {
				GraphicsUtils.drawLine(sprite.graphics, origin.x, origin.y, _destination.x, _destination.y);
			}
			super.init();
		}
		
		public function set destination(p:Point):void {
			this._destination = p;
		}
		
		public function get destination():Point {
			return this._destination;
		}
		
		public function set dashed(d:Boolean):void {
			this._dashed = true;
		}
		
		public function get dashed():Boolean {
			return _dashed;
		}
		
		public override function moveTo(point:Point):void {
			super.moveTo(point);
			this.destination = destination.plus(delta);
		}
	}
}